<?php

	class collocate_goods_listener extends pageFactory
	{
		function collocate_goods_listener()
		{
			parent::pageFactory();
			$this->system = &$GLOBALS['system'];
			$this->db = &$this->system->database();
		}

		function goodsSave($args,$data)
		{
			if ($data['collocation'])
			{
				$pValue = $data['collocation'];
				$objGoods = $this->system->loadModel('trading/goods');
				//处理搭配商品
				foreach ($data['collocation']['name'] as $key => $name)
				{
					$aItem['name'] = $name;
					$aItem['type'] = $data['collocation']['type'][$key];
					$aItem['min_num'] = $data['collocation']['min_num'][$key];
					$aItem['max_num'] = $data['collocation']['max_num'][$key];
					$aItem['set_price'] = $data['collocation']['set_price'][$key];
					$aItem['price'] = $data['collocation']['price'][$key];
					$aItem['isBind'] = $data['collocation']['isBind'][$key];
					$aItem['isShowOriPrice'] = $data['collocation']['isShowOriPrice'][$key];
					$aItem['isShowSaving'] = $data['collocation']['isShowSaving'][$key];
					$aItem['items'] = $data['collocation']['items'][$key];
					$aCollacation[$key] = $aItem;
				}
				$objGoods->setGoodsMemo($data['goods_id'],'collocation',$aCollacation);
			}
		}

		function orderCreate($args,$data)
		{
			//error_log(print_r(func_get_args(),true),3,__FILE__.'.'.__FUNCTION__.'.func_get_args.log');//alone add
			//通过前台创建的订单 需要清空搭配商品规则
			$objCollocAddon = $this->system->loadModel('plugins/collocate_goods/collocAddon');	
			
			//在collocorder表中记录搭配商品规则
			if($data['member_id']){
				$sCookie = $objCollocAddon->getMemColloc($data['member_id']);
			}else{
				$sCookie = $_COOKIE[$objCollocAddon->coCookie];
			}
			if($sCookie){
				$aData['order_id'] = $data['order_id'];
				$aData['addon'] = $sCookie;
				$sql = "SELECT * FROM sdb_collocate_goods_collocorder WHERE order_id='". $data['order_id']."'";
				$aRs = $this->db->query($sql);
				$sSql = $this->db->GetUpdateSql($aRs,$aData,true);
				$this->db->exec($sSql);
				
				//插入items到sdb_collocate_goods_order_items表中 后台订单编辑用
				$coTrading = $objCollocAddon->getShopexCartFormat(&$collocItems);
				foreach ($coTrading as $key =>$v)
				{
					if (!empty($v['products']) && is_array($v['products']))
					{
						foreach ($v['products'] as $k =>$aItems)
						{
							//主商品的p_id为0 其余为$key
							if (0 == $k){
								$aItems['p_id'] = '0';
								$aItems['co_id'] = $key;
							}
							else{
								$aItems['p_id'] = $key;
								$aItems['co_id'] = '0';
							}
							$aItems['order_id'] = $data['order_id'];
							$aItems['addon'] = serialize($aItems['addon']);
							$aItems['minfo'] = serialize($data['minfo']);
							//$aItems['price'] = $aItems['_pmt']['price'];
							//$aItems['amount'] = $aItems['_pmt']['amount'];

							$rs = $this->db->query('SELECT * FROM sdb_collocate_goods_order_items WHERE 0=1');
							$sqlString = $this->db->GetInsertSQL($rs, $aItems);
							if($sqlString) $this->db->exec($sqlString);
							
							$objGoods = $this->system->loadModel('trading/goods');
							$objOrder = $this->system->loadModel('trading/order');
							$objGoods->updateRank($aItems['goods_id'], 'buy_count', $aItems['nums']);    //购买次数统计
							//冻结库存
							if($objOrder->freez_time()=='order'){
								if(intval($product['nums'])>=0){
									$this->db->exec("UPDATE sdb_products SET freez = freez + ".intval($aItems['nums'])." WHERE product_id = ".intval($aItems['product_id']));
								}
								$this->db->exec("UPDATE sdb_products SET freez = ".intval($aItems['nums'])." WHERE product_id = ".intval($aItems['product_id'])." AND freez IS NULL");
							}
						}
					}
				}
			}

			//订单完成 清除数据中的addon
			list($ident,$ctl,$method) = explode(':',$this->system->request['action']['ident']);
			if ('shop' == $ident)
			{
				if ($data['member_id'])
				{
					$sql = 'UPDATE sdb_collocate_goods_collocaddon SET addon="" WHERE member_id='.$data['member_id'];
					$this->db->exec($sql);
				}
				else $this->system->setcookie($objCollocAddon->coCookie,'');
			}
		}

		function editOrder($type,$aData)
		{
			//error_log(print_r(func_get_args(),true),3,__FILE__.'.'.__FUNCTION__.'.func_get_args.log');//alone add
			$orderid = $aData['order_id'];
			$objCollocOrder = &$this->system->loadModel('plugins/collocate_goods/collocOrder');
			//搭配商品总金额
			$coItemsAmount = $objCollocOrder->getCoItemsAmount($orderid);
			$aField = $objCollocOrder->getFieldById($orderid,array('cost_item','total_amount','final_amount','cur_rate'));

			$updateData['cost_item'] = $aField['cost_item'] + $coItemsAmount;
			$updateData['total_amount'] = $aField['total_amount'] + $coItemsAmount;
			$updateData['final_amount'] = $aField['final_amount'] + $coItemsAmount*$aField['cur_rate'];

			$rs = $this->db->query('SELECT * FROM sdb_orders WHERE order_id="'.$orderid.'"');
			$updateSql = $this->db->GetUpdateSql($rs,$updateData);
			$this->db->exec($updateSql);
		}

	}

?>